package com.jprocms.module.cms.controller.admin.link;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jprocms.module.cms.controller.admin.link.vo.*;
import com.jprocms.module.cms.convert.link.LinkConvert;
import com.jprocms.module.cms.dal.dataobject.link.LinkDO;
import com.jprocms.module.cms.service.link.LinkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;



@Tag(name = "管理后台 - 友情链接")
@RestController
@RequestMapping("/cms/link")
@Validated
public class LinkController {

    @Resource
    private LinkService linkService;

    @PostMapping("/create")
    @Operation(summary = "创建友情链接")
    @PreAuthorize("@ss.hasPermission('cms:link:create')")
    public CommonResult<Long> createLink(@Valid @RequestBody LinkCreateReqVO createReqVO) {
        return success(linkService.createLink(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新友情链接")
    @PreAuthorize("@ss.hasPermission('cms:link:update')")
    public CommonResult<Boolean> updateLink(@Valid @RequestBody LinkUpdateReqVO updateReqVO) {
        linkService.updateLink(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除友情链接")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:link:delete')")
    public CommonResult<Boolean> deleteLink(@RequestParam("id") Long id) {
        linkService.deleteLink(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得友情链接")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:link:query')")
    public CommonResult<LinkRespVO> getLink(@RequestParam("id") Long id) {
        LinkDO link = linkService.getLink(id);
        return success(LinkConvert.INSTANCE.convert(link));
    }

    @GetMapping("/list")
    @Operation(summary = "获得友情链接列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:link:query')")
    public CommonResult<List<LinkRespVO>> getLinkList(@RequestParam("ids") Collection<Long> ids) {
        List<LinkDO> list = linkService.getLinkList(ids);
        return success(LinkConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得友情链接分页")
    @PreAuthorize("@ss.hasPermission('cms:link:query')")
    public CommonResult<PageResult<LinkRespVO>> getLinkPage(@Valid LinkPageReqVO pageVO) {
        PageResult<LinkDO> pageResult = linkService.getLinkPage(pageVO);
        return success(LinkConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出友情链接 Excel")
    @PreAuthorize("@ss.hasPermission('cms:link:export')")

    public void exportLinkExcel(@Valid LinkListReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        List<LinkDO> list = linkService.getLinkList(exportReqVO);
        // 导出 Excel
        List<LinkExcelVO> datas = LinkConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "友情链接.xls", "数据", LinkExcelVO.class, datas);
    }

}
